<template>
  <div id="twotype">
    <div class="tables">
      <el-table
        :data="tableData"
        class="table"
        height="100%"
        ref="jdjgTable"
        @sort-change="sortChange"
        :header-cell-style="{
          padding: '0px',
          background: 'linear-gradient(0deg, #F4F6F9, #EAECF4, #F3F6F9)'
        }"
        :row-class-name="tabRowClassName"
        header-row-class-name="mapTable_th"
      >
        <el-table-column width="60" align="center" type="index" :index="addIndex" label="序号">
          <el-table-column width="60" align="center" type="index" label="检索"> </el-table-column>
        </el-table-column>
        <el-table-column label="一级品类" align="center">
          <el-table-column align="center" sortable="custom" prop="fatherName">
            <template slot="header" align="center" slot-scope="{}">
              <div class="searchBox" @click.stop>
                <el-select v-model.trim="searchQrcoUsed" placeholder="请选择" @change="getDataChange">
                  <el-option label="全部" :value="null"></el-option>
                  <el-option label="已使用" :value="1"></el-option>
                  <el-option label="未使用" :value="2"></el-option>
                </el-select>
              </div>
            </template>
          </el-table-column>
        </el-table-column>
        <el-table-column label="二级品类" align="center">
          <el-table-column align="center" sortable="custom" prop="typeName">
            <template slot="header" align="center" slot-scope="{}">
              <div class="searchBox" @click.stop>
                <el-select v-model.trim="searchQrcoUsed" placeholder="请选择" @change="getDataChange">
                  <el-option label="全部" :value="null"></el-option>
                  <el-option label="已使用" :value="1"></el-option>
                  <el-option label="未使用" :value="2"></el-option>
                </el-select>
              </div>
            </template>
          </el-table-column>
        </el-table-column>

        <el-table-column label="状态" align="center">
          <el-table-column align="center" prop="state" width="120">
            <template slot-scope="scope">
              <div>{{ scope.row.state ? '正常' : '禁用' }}</div>
            </template>
          </el-table-column>
        </el-table-column>
        <el-table-column label="录入人" align="center">
          <el-table-column align="center" sortable="custom" prop="createBy">
            <template slot="header" align="center" slot-scope="{}">
              <div class="searchBox" @click.stop>
                <el-input v-model.trim="searchPositionName" @change="getDataChange"></el-input>
              </div>
            </template>
          </el-table-column>
        </el-table-column>
        <el-table-column label="录入日期" align="center">
          <el-table-column width="180" align="center" sortable="custom" prop="createTime">
            <template slot="header" align="center" slot-scope="{}">
              <div class="searchBox" @click.stop>
                <el-input v-model.trim="searchPositionName" @change="getDataChange"></el-input>
              </div>
            </template>
          </el-table-column>
        </el-table-column>
        <el-table-column label="操作" align="center" width="200">
          <el-table-column align="center" width="200">
            <template slot-scope="scope">
              <div class="">
                <el-button type="text" class="xg czBtn" size="small" @click="handleClick('修改', scope.row)">
                  修改
                </el-button>
                <el-button type="text" class="ty czBtn" size="small" @click="handleClick('删除', scope.row)">
                  删除
                </el-button>
                <el-button
                  v-if="scope.row.state"
                  type="text"
                  class="ty czBtn"
                  size="small"
                  @click="handleClick('停用', scope.row)"
                >
                  停用
                </el-button>
                <el-button v-else type="text" class="xg czBtn" size="small" @click="handleClick('启用', scope.row)">
                  启用
                </el-button>
              </div>
            </template>
          </el-table-column>
        </el-table-column>
      </el-table>
    </div>
    <div class="pags">
      <element-pagination @getData="getPagChange" :Total="total"> </element-pagination>
    </div>
    <el-dialog
      width="442px"
      class="model-dialog"
      v-if="updateVisible"
      :center="true"
      :close-on-click-modal="false"
      title="修改二级品类"
      :before-close="closeDialog"
      :visible.sync="updateVisible"
    >
      <el-form ref="form" class="infoForm" label-width="80px">
        <el-form-item label="一级品类:">
          <el-input v-model.trim="oneTypeName" size="mini" disabled class="dsiabled"> </el-input>
        </el-form-item>
        <el-form-item label="二级品类:">
          <el-input v-model.trim="twoTypeName" size="mini"> </el-input>
        </el-form-item>
      </el-form>
      <dialog-footer-button @close="close()" @saveName="saveName()"> </dialog-footer-button>
    </el-dialog>
  </div>
</template>

<script>
import dialogFooterButton from '../../../../components/dialogFooterButton.vue'
import ElementPagination from '../../../../components/ElementPagination.vue'
export default {
  data() {
    return {
      oneTypeName: '',
      twoId: null,
      twoTypeName: '',
      updateVisible: false,
      searchQrcoUsed: '',
      searchPositionName: '',
      tableData: [],
      total: 0,
      size: 15,
      page: 1
    }
  },
  mounted() {
    this.getData()
  },
  methods: {
    closeDialog(done) {
      this.twoId = null
      this.oneTypeName = ''
      this.twoTypeName = ''
      done()
    },
    saveName() {
      if (this.twoTypeName && this.twoTypeName.length < 12) {
        this.$https({
          method: 'post',
          url: '/material/alterTypeTwo',
          data: {
            id: this.twoId,
            typeName: this.twoTypeName
          }
        })
          .then(res => {
            if (res.code === 0) {
              this.$message.success('修改成功')
              this.getData()
              this.close()
            } else {
              this.$message.error(res.msg)
            }
          })
          .catch(err => {
            console.log(err)
          })
      } else {
        this.$message.error('请输入12位以内的二级品类名称')
      }
    },
    close() {
      this.twoId = null
      this.oneTypeName = ''
      this.twoTypeName = ''
      this.updateVisible = false
    },
    handleClick(val, scope) {
      if (val === '修改') {
        this.twoId = scope.id
        this.oneTypeName = scope.fatherName
        this.twoTypeName = scope.typeName
        this.updateVisible = true
      } else if (val === '删除') {
        this.$confirm('是否删除该二级品类', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        })
          .then(() => {
            this.delTwoType(scope.id)
          })
          .catch(() => {
            this.$message('取消删除')
          })
      } else if (val === '停用') {
        this.$confirm('是否停用该二级品类', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        })
          .then(() => {
            this.disableTwoType(scope.id)
          })
          .catch(() => {
            this.$message('取消')
          })
      } else if (val === '启用') {
        this.$confirm('是否启用该二级品类', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        })
          .then(() => {
            this.enableTwoType(scope.id)
          })
          .catch(() => {
            this.$message('取消')
          })
      }
    },
    // 删除二级品类
    delTwoType(id) {
      let data = [
        {
          id
        }
      ]
      this.$https({
        method: 'post',
        url: '/material/deleteTypeTwo',
        data: data
      })
        .then(res => {
          if (res.code === 0) {
            this.$message.success('删除成功')
            this.getData()
          } else {
            this.$message.error(res.msg)
          }
        })
        .catch(err => {
          console.log(err)
        })
    },
    // 启用二级品类
    enableTwoType(id) {
      let data = [{ id }]
      this.$https({
        method: 'post',
        url: '/material/enableTypeTwo',
        data: data
      })
        .then(res => {
          if (res.code === 0) {
            this.$message.success('启用成功')
            this.getData()
          } else {
            this.$message.error(res.msg)
          }
        })
        .catch(err => {
          console.log(err)
        })
    },
    // 禁用二级品类
    disableTwoType(id) {
      let data = [{ id }]
      this.$https({
        method: 'post',
        url: '/material/disableTypeTwo',
        data: data
      })
        .then(res => {
          if (res.code === 0) {
            this.$message.success('禁用成功')
            this.getData()
          } else {
            this.$message.error(res.msg)
          }
        })
        .catch(err => {
          console.log(err)
        })
    },
    getDataChange() {},
    getPagChange(data) {
      this.page = data.currentPage
      this.size = data.pagesize
      this.getData()
    },
    getData() {
      this.$https({
        url: '/material/listTypeOneOrTwoByOptions',
        method: 'post',
        data: {
          createBy: '',
          createTime: '',
          page: this.page,
          size: this.size,
          state: null,
          typeDj: 2,
          typeName: ''
        }
      })
        .then(res => {
          if (res.code === 0) {
            this.total = res.data.total
            this.tableData = res.data.records
          } else {
            this.$message.error(res.msg)
          }
        })
        .catch(err => {
          console.log(err)
        })
    },
    sortChange() {},
    addIndex(index) {
      return index + 1 + (this.page - 1) * this.size
    },
    tabRowClassName({ row, rowIndex }) {
      let index = rowIndex + 1
      if (index % 2 == 0) {
        return 'warning-row'
      }
    }
  },
  components: { ElementPagination, dialogFooterButton }
}
</script>
<style lang="less" scoped>
#twotype {
  height: 100%;
  .tables {
    height: calc(100% - 62px);
  }
}
.xg {
  color: #166df1;
}
.ty {
  color: #ff3d3d;
}
</style>
